package com.nonintrusive.client;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Dialog;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nonintrusive.client.database.ClientDB;
import com.nonintrusive.client.send.R;
import com.nonintrusive.message.MessageNonIntrusive;

public class QuestionDialog extends Dialog {

	private static final String TAG = "QuestionDialog";

	// private Question mQuestion;
	private TextView tvQ;

	// private LinearLayout layout1;
	// private LinearLayout layout2;
	private LinearLayout layout3;
	private LinearLayout layout4;
	private LinearLayout layout5;

	private TextView tvA1;
	private TextView tvA2;
	private TextView tvA3;
	private TextView tvA4;
	private TextView tvA5;

	private Button btnA1;
	private Button btnA2;
	private Button btnA3;
	private Button btnA4;
	private Button btnA5;

	private boolean readOnly;

	private Context context;

	public QuestionDialog(Context context, boolean readOnly) {
		super(context);
		setContentView(R.layout.q_dlg);
		setCancelable(true);
		setCanceledOnTouchOutside(true);
		setTitle("Question Info");
		this.readOnly = readOnly;
		this.context = context;

		tvQ = (TextView) findViewById(R.id.q_message);

		// layout1 = (LinearLayout) findViewById(R.id.q_layout_a1);
		// layout2 = (LinearLayout) findViewById(R.id.q_layout_a2);
		layout3 = (LinearLayout) findViewById(R.id.q_layout_a3);
		layout4 = (LinearLayout) findViewById(R.id.q_layout_a4);
		layout5 = (LinearLayout) findViewById(R.id.q_layout_a5);

		tvA1 = (TextView) findViewById(R.id.q_a1);
		tvA2 = (TextView) findViewById(R.id.q_a2);
		tvA3 = (TextView) findViewById(R.id.q_a3);
		tvA4 = (TextView) findViewById(R.id.q_a4);
		tvA5 = (TextView) findViewById(R.id.q_a5);

		btnA1 = (Button) findViewById(R.id.a1Btn);
		btnA2 = (Button) findViewById(R.id.a2Btn);
		btnA3 = (Button) findViewById(R.id.a3Btn);
		btnA4 = (Button) findViewById(R.id.a4Btn);
		btnA5 = (Button) findViewById(R.id.a5Btn);
	}

	public void setQuestion(final Question question) {
		// mQuestion = question;

		tvQ.setText(question.message);

		tvA1.setText(question.answer1);
		tvA2.setText(question.answer2);
		if (question.answer3 != null) {
			tvA3.setText(question.answer3);
			layout3.setVisibility(View.VISIBLE);
			if (question.answer4 != null) {
				tvA4.setText(question.answer4);
				layout4.setVisibility(View.VISIBLE);
				if (question.answer5 != null) {
					tvA5.setText(question.answer5);
					layout5.setVisibility(View.VISIBLE);
				} else {
					layout5.setVisibility(View.GONE);
				}
			} else {
				layout4.setVisibility(View.GONE);
				layout5.setVisibility(View.GONE);
			}
		} else {
			layout3.setVisibility(View.GONE);
			layout4.setVisibility(View.GONE);
			layout5.setVisibility(View.GONE);
		}

		if (!readOnly) {
			if (!question.isMine) {
				if (!question.isAnswered) {
					btnA1.setEnabled(true);
					btnA2.setEnabled(true);
					btnA3.setEnabled(true);
					btnA4.setEnabled(true);
					btnA5.setEnabled(true);
					// TODO: mark the button for isAnswered
					btnA1.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							sendAnswerToServer(question, 1);
							cancel();
						}

					});
					btnA2.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							sendAnswerToServer(question, 2);
							cancel();
						}
					});
					btnA3.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							sendAnswerToServer(question, 3);
							cancel();
						}
					});
					btnA4.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							sendAnswerToServer(question, 4);
							cancel();
						}
					});
					btnA5.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							sendAnswerToServer(question, 5);
							cancel();
						}
					});
				} else {
					btnA1.setEnabled(false);
					btnA2.setEnabled(false);
					btnA3.setEnabled(false);
					btnA4.setEnabled(false);
					btnA5.setEnabled(false);
				}
			} else {
				// TODO
				// do something with %for each answer
			}
		} else {
			btnA1.setEnabled(false);
			btnA2.setEnabled(false);
			btnA3.setEnabled(false);
			btnA4.setEnabled(false);
			btnA5.setEnabled(false);
		}
		Button closeBtn = (Button) findViewById(R.id.q_close);
		closeBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				cancel();
			}
		});
	}

	private void sendAnswerToServer(Question question, int answer) {
		ContentResolver cr = context.getContentResolver();
		ContentValues values = new ContentValues();
		// TODO
		// 1. save in DB

		// 2. send it now
		try {
			Socket socket = new Socket(GlobalNonIntrusive.SERVER_IP,
					GlobalNonIntrusive.PORT);
			ObjectOutputStream oos = new ObjectOutputStream(
					socket.getOutputStream());

			ObjectInputStream ois = new ObjectInputStream(
					socket.getInputStream());

			// send qType = ANY_QUESTIONS
			Log.d(TAG, "send ANSWER");
			MessageNonIntrusive sendMessage = new MessageNonIntrusive();
			sendMessage.type = MessageNonIntrusive.ANSWER_TYPE;
			synchronized (GlobalNonIntrusive.clientID) {
				sendMessage.senderId = GlobalNonIntrusive.clientID;
			}
			sendMessage.content = "" + answer; // answer nr
			sendMessage.questionId = question.id;
			oos.writeObject(sendMessage);
			ois.close();
			oos.close();
			socket.close();

			values.put(ClientDB.IS_ANSWERED, true);
			values.put(ClientDB.TIMESTAMP, System.currentTimeMillis());
			cr.update(ClientDB.CONTENT_URI, values, ClientDB.QUESTION_ID
					+ " = " + question.id, null);

		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
